//
//  HomeMeAnswerController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/18.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeMeAnswerController.h"
#import "AnswerDetailTwoController.h"


@interface HomeMeAnswerController ()
/**收藏*/
@property (nonatomic,strong)UIButton*oneButton;
/**提问*/
@property (nonatomic,strong)UIButton*twoButton;
/**回答*/
@property (nonatomic,strong)UIButton*thereButton;
/**底部线条*/
@property (nonatomic,strong)UIView*oneLine;
/**底部线条*/
@property (nonatomic,strong)UIView*twoLine;
/**底部线条*/
@property (nonatomic,strong)UIView*thereLine;

/**类型及数量*/
@property (nonatomic,strong)UILabel*clasNameLb;

/**listViewTool*/
@property (nonatomic,strong)UITableViewTool*listViewTool;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataSoure;

@end

@implementation HomeMeAnswerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的回答";
    [self createUI];
}

-(void)createUI{
    UIView * view = [self createHeaderview];
    [self.view addSubview:view];
    
    UIView * lineView = UIView.new;
    lineView.backgroundColor = UIColorFromRGB(0xF5F7F5);
    
    [self.view addSubview:lineView];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(view.mas_bottom);
        make.height.mas_equalTo(8);
    }];
    
    UIView * swicheView = [self createSwichtView];
    
    [self.view addSubview:swicheView];
    
    [swicheView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(lineView.mas_bottom);
    }];
    UIView * twolineView = UIView.new;
    twolineView.backgroundColor = UIColorFromRGB(0xF5F7F5);

    [self.view addSubview:twolineView];
    [self.view addSubview:self.clasNameLb];
    
    [twolineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(swicheView.mas_bottom);
        make.height.mas_equalTo(8);
    }];
    
    [self.clasNameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(twolineView.mas_bottom).offset(16);
    }];
    
    [self.listViewTool.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.clasNameLb.mas_bottom).offset(16);
    }];
}

#pragma mark --懒加载

-(UITableViewTool*)listViewTool{
    if(!_listViewTool){
        kWeakSelf(weakSelf);
        _listViewTool=[[UITableViewTool alloc] initWithFrame:CGRectZero style:UITableViewStylePlain registerCellClassName:NSStringFromClass([HomeMeAnswerCell class]) identifier:NSStringFromClass([HomeMeAnswerCell class]) dataArray:self.dataSoure delegate:self dataSourceBlock:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            HomeMeAnswerCell * cell = (HomeMeAnswerCell *)TempCell;
            if (weakSelf.oneButton.selected) {
                cell.oneLabel.text = @"现代人大多处于亚健康，与饮食的关系？";
                cell.twoLabel.text = @"";
                cell.thereLabel.text = @"0个回答";
            }else if (weakSelf.twoButton.selected){
                cell.oneLabel.text = @"现代人大多处于亚健康，与饮食的关系？";
                cell.twoLabel.text = @"";
                cell.thereLabel.text = @"0个回答";
            }else if (weakSelf.thereButton.selected){
                cell.oneLabel.text = @"西红柿炒蛋先放西红柿还是先放鸡蛋？";
                cell.twoLabel.text = @"一般我炒的时候，是先将鸡蛋打散，下锅炒至7成熟，盛出，倒油放西红柿翻炒至7成熟，导入刚刚炒好的…";
                cell.thereLabel.text = @"1234赞  1234阅读";
            }
            
            
        } selectCell:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            
            if (weakSelf.thereButton.selected) {
                AnswerDetailTwoController * vc = [AnswerDetailTwoController new];
                vc.type = AnswerDetailMe;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            
        }];
        _listViewTool.listView.tableFooterView = UIView.new;
    }
    return _listViewTool;
}
-(UILabel*)clasNameLb{
    if(!_clasNameLb){
        _clasNameLb=[UILabel new];
        _clasNameLb.textColor = UIColorFromRGB(0x454F44);
        _clasNameLb.font = PingFangSC_Medium(14);
        _clasNameLb.text = @"收藏问题（4）";

    }
    return _clasNameLb;
}
-(UIView *)createSwichtView{
    UIView * view = [[UIView alloc] initWithFrame:CGRectZero];
    view.userInteractionEnabled = YES;
    
    self.oneLine = [UIView new];
    self.oneLine.backgroundColor = UIColorFromRGB(0x009966);
    
    self.twoLine = [UIView new];
    self.twoLine.backgroundColor = UIColorFromRGB(0x009966);
    
    self.thereLine = [UIView new];
    self.thereLine.backgroundColor = UIColorFromRGB(0x009966);

    [view addSubview:self.oneButton];
    [view addSubview:self.twoButton];
    [view addSubview:self.thereButton];
    [view addSubview:self.oneLine];
    [view addSubview:self.twoLine];
    [view addSubview:self.thereLine];

    self.oneLine.hidden = !self.oneButton.selected;
    self.twoLine.hidden = !self.twoButton.selected;
    self.thereLine.hidden = !self.thereButton.selected;

    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(40);
    }];
    [self.oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.oneButton);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(20);
        make.top.mas_equalTo(self.oneButton.mas_bottom).offset(1);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(self.oneButton.mas_right).offset(40);
        make.width.mas_equalTo(40);

    }];
    
    [self.twoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.twoButton);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(20);
        make.top.mas_equalTo(self.twoButton.mas_bottom).offset(1);
    }];
    
    [self.thereButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(self.twoButton.mas_right).offset(40);
        make.width.mas_equalTo(40);
    }];
    
    [self.thereLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.thereButton);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(20);
        make.top.mas_equalTo(self.thereButton.mas_bottom).offset(1);
    }];

    return view;
}
-(UIButton*)oneButton{
    if(!_oneButton){
        _oneButton=[self createButton:@"收藏"];
        _oneButton.selected = YES;
        kWeakSelf(weakSelf);
        [_oneButton addActionHandler:^{
            weakSelf.clasNameLb.text = @"收藏问题（4）";

            weakSelf.oneButton.selected = YES;
            weakSelf.oneLine.hidden = NO;
            
            weakSelf.twoButton.selected = NO;
            weakSelf.twoLine.hidden = YES;
            weakSelf.thereButton.selected = NO;
            weakSelf.thereLine.hidden = YES;
            
            [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

        }];
    }
    return _oneButton;
}
-(UIButton*)twoButton{
    if(!_twoButton){
        _twoButton=[self createButton:@"提问"];
        _twoButton.selected = NO;
        kWeakSelf(weakSelf);
        [_twoButton addActionHandler:^{
            weakSelf.clasNameLb.text = @"我的提问（3）";
            weakSelf.oneButton.selected = NO;
            weakSelf.oneLine.hidden = YES;
            
            weakSelf.twoButton.selected = YES;
            weakSelf.twoLine.hidden = NO;
            weakSelf.thereButton.selected = NO;
            weakSelf.thereLine.hidden = YES;
            
            [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

        }];
    }
    return _twoButton;
}

-(UIButton*)thereButton{
    if(!_thereButton){
        _thereButton=[self createButton:@"回答"];
        _thereButton.selected = NO;
        kWeakSelf(weakSelf);
        [_thereButton addActionHandler:^{
            weakSelf.clasNameLb.text = @"我的回答（2）";

            weakSelf.oneButton.selected = NO;
            weakSelf.oneLine.hidden = YES;
            
            weakSelf.twoButton.selected = NO;
            weakSelf.twoLine.hidden = YES;
            weakSelf.thereButton.selected = YES;
            weakSelf.thereLine.hidden = NO;
            
            [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

        }];
        
    }
    return _thereButton;
}
-(UIButton *)createButton:(NSString *)title{
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateSelected];
    [button setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    button.titleLabel.font = SYSTEMFONT(16);
    
    return button;
}
-(UIView *)createHeaderview{
    UIView * view = UIView.new;
    
    UIImageView * oneImage = UIImageView.new;
    oneImage.image = IMAGE_NAMED(@"huida_yanjin");
    
    UIImageView * twoImage = UIImageView.new;
    twoImage.image = IMAGE_NAMED(@"ic_like_defut");

    UILabel * oneTitle = [self createTitleLabel:@"回答获2345人阅读"];
    UILabel * twoTitle = [self createTitleLabel:@"回答获432人点赞"];
    
    UILabel * oneBtomLabel = [self createLabel:@"本月共1111人浏览"];
    UILabel * twoBtomLabel = [self createLabel:@"本月共233人点赞"];

    
    [view addSubview:oneImage];
    [view addSubview:twoImage];
    [view addSubview:oneTitle];
    [view addSubview:twoTitle];
    [view addSubview:oneBtomLabel];
    [view addSubview:twoBtomLabel];

    [oneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(23);
        make.width.height.mas_equalTo(16);
    }];
    [twoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(oneImage.mas_bottom).offset(58);
        make.width.height.mas_equalTo(16);
    }];
    [oneTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(oneImage.mas_right).offset(12);
        make.centerY.mas_equalTo(oneImage);
        make.right.mas_equalTo(-16);
    }];
    [twoTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(twoImage.mas_right).offset(12);
        make.centerY.mas_equalTo(twoImage);
        make.right.mas_equalTo(-16);
    }];
    [oneBtomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(oneTitle);
        make.top.mas_equalTo(oneTitle.mas_bottom).offset(2);
        make.right.mas_equalTo(-16);
    }];
    [twoBtomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(twoTitle);
        make.top.mas_equalTo(twoTitle.mas_bottom).offset(2);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-16);
    }];

    return view;
    
}
-(UILabel *)createTitleLabel:(NSString *)text{
    UILabel * lable = UILabel.new;
    lable.textColor = UIColorFromRGB(0x273323);
    lable.font = PingFangSC_Medium(16);
    lable.text = text;
    
    return lable;
}
-(UILabel *)createLabel:(NSString *)text{
    UILabel * lable = UILabel.new;
    lable.textColor = UIColorFromRGB(0x959E97);
    lable.font = PingFangSC_Medium(12);
    lable.text = text;
    
    return lable;
}

-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        
        _dataSoure =[[NSMutableArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"", nil];
    }
    return _dataSoure;
}

@end


@implementation HomeMeAnswerCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)createUI{
    [self addSubview:self.oneLabel];
    [self addSubview:self.twoLabel];
    [self addSubview:self.thereLabel];
    
    [self.oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.oneLabel.mas_bottom).offset(8);
    }];
    [self.thereLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.bottom.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.twoLabel.mas_bottom).offset(8);
    }];

}
#pragma mark ---懒加载
-(UILabel*)oneLabel{
    if(!_oneLabel){
        _oneLabel=[UILabel new];
        _oneLabel.textColor = UIColorFromRGB(0x273323);
        _oneLabel.font = BOLDSYSTEMFONT(18);
    }
    return _oneLabel;
}
-(UILabel*)twoLabel{
    if(!_twoLabel){
        _twoLabel=[UILabel new];
        _twoLabel.textColor = UIColorFromRGB(0x454F44);
        _twoLabel.font = BOLDSYSTEMFONT(14);
        _twoLabel.numberOfLines = 2;
    }
    return _twoLabel;
}
-(UILabel*)thereLabel{
    if(!_thereLabel){
        _thereLabel=[UILabel new];
        _thereLabel.textColor = UIColorFromRGB(0x959E97);
        _thereLabel.font = BOLDSYSTEMFONT(12);
    }
    return _thereLabel;
}

@end
